package com.zzptc.fxh.danmu;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import master.flame.danmaku.controller.DrawHandler;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.DanmakuTimer;
import master.flame.danmaku.danmaku.model.IDanmakus;
import master.flame.danmaku.danmaku.model.android.DanmakuContext;
import master.flame.danmaku.danmaku.model.android.Danmakus;
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser;
import master.flame.danmaku.ui.widget.DanmakuView;

public class MainActivity extends AppCompatActivity {
    VideoView shipin;
    Button startbtn;
    DanmakuView danmakuView;
    EditText danmuedit;
    LinearLayout linearLayout;
    DanmakuContext danmakuContext;

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        event();
        initDanmu();
    }
    //创建弹幕解析器
    private BaseDanmakuParser parser=new BaseDanmakuParser() {
        @Override
        protected IDanmakus parse() {
            return new Danmakus();
        }
    };
    private void initDanmu() {
        danmakuView.setCallback(new DrawHandler.Callback() {
            @Override
            public void prepared() {
                danmakuView.start();
            }

            @Override
            public void updateTimer(DanmakuTimer timer) {

            }

            @Override
            public void danmakuShown(BaseDanmaku danmaku) {

            }

            @Override
            public void drawingFinished() {

            }
        });
        danmakuContext=DanmakuContext.create();
        danmakuView.enableDanmakuDrawingCache(true);//提升绘制效率
        danmakuView.prepare(parser,danmakuContext);
        //为弹幕设置点击事件
        danmakuView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (linearLayout.getVisibility()==View.GONE){
                    linearLayout.setVisibility(View.VISIBLE);
                }else {
                    linearLayout.setVisibility(View.GONE);
                }
            }
        });
    }
    public void event(){
        startbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Log.d(TAG, "onClick: 播放");

                String uri="android.resource://"+getPackageName()+"/"+R.raw.bujushipin;
                if (uri!=null){
                    shipin.setVideoURI(Uri.parse(uri));
                    shipin.start();
                    shipin.setMediaController(new MediaController(MainActivity.this));
                }
            }
        });
    }

    public void init() {
        shipin=findViewById(R.id.shipin);
        startbtn=findViewById(R.id.startbtn);
        danmakuView=findViewById(R.id.danmu);
        danmuedit=findViewById(R.id.danmuedit);
        linearLayout=findViewById(R.id.lit);
    }

    public void send(View view) {
        //收集发送的内容
        String s=danmuedit.getText().toString().trim();
        if (s.equals("")){
            Toast.makeText(this,"",Toast.LENGTH_SHORT).show();
        }else {
            sendDanmu(s,true);
        }
    }

    private void sendDanmu(String s, boolean b) {
        BaseDanmaku danmaku=danmakuContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL);
        danmaku.text=s;
        danmaku.textColor=500;
        danmaku.textColor= Color.BLUE;
        if (b){
            danmaku.borderColor=Color.YELLOW;
        }
        danmaku.setTime(danmakuView.getCurrentTime());
        danmakuView.addDanmaku(danmaku);
    }
}
